package com.nexus.service.sys.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nexus.core.exception.ServiceException;
import com.nexus.core.service.BaseService;
import com.nexus.core.sys.entity.SysRole;
import com.nexus.core.sys.dto.SysRoleDTO;
import com.nexus.core.utils.ObjectUtils;
import com.nexus.service.sys.mapper.SysRoleMapper;
import com.nexus.service.sys.service.mapstruct.SysRoleWrapper;
import org.apache.ibatis.annotations.CacheNamespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 系统角色Service
 *
 * @author 路正宁
 * @version 2023-03-16
 */
//@DS("zxhy_platform_db")
//@Service
@Transactional
@CacheNamespace
public class SysRoleService extends BaseService<SysRoleMapper, SysRole,SysRoleDTO,SysRoleWrapper> {

    @Autowired
    SysRoleWrapper sysRoleWrapperImpl;
    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public SysRoleDTO findById(String id) {
        return baseMapper.findById(id);
    }

    /**
     * 自定义分页检索
     *
     * @param page
     * @param queryWrapper
     * @return
     */
    public IPage<SysRoleDTO> findPage(Page<SysRoleDTO> page, QueryWrapper queryWrapper) {
        queryWrapper.eq("a.deleted", 0); // 排除已经删除
        return baseMapper.findList(page, queryWrapper);
    }

    /**
     * 根据机构Ids查询
     * @Date 2023/9/6 9:56
     * @Author luzhengning
     **/
    public IPage<SysRoleDTO> findByOrgIds(List<String> orgIds) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.in("a.sys_org", orgIds);
        queryWrapper.eq("a.deleted",0);
        return baseMapper.findList(new Page<>(1,1000), queryWrapper);
    }

    public List<SysRoleDTO> findByOrgId(String orgId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("a.sys_org", orgId);
        queryWrapper.eq("a.deleted",0);
        List<SysRoleDTO> records = baseMapper.findList(new Page<>(1, 1000), queryWrapper).getRecords();
        return records;
    }
    /**
     * 查询机构下指定的角色
     * @Date 2024/6/11 15:24
     * @Author luzhengning
     **/
    public SysRoleDTO findByOrgAndEnName(String orgId,String roleName) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("a.sys_org", orgId);
        queryWrapper.eq("a.en_name", roleName);
        queryWrapper.eq("a.deleted",0);
        IPage<SysRoleDTO> list = baseMapper.findList(new Page<>(1, 1), queryWrapper);
        if(list!=null&&list.getRecords()!=null&&list.getRecords().size()>0){
            return list.getRecords().getFirst();
        }
        return null;
    }
    public SysRoleDTO findByOrgAndRoleType(String orgId,String roleType) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("a.sys_org", orgId);
        queryWrapper.eq("a.role_type", roleType);
        queryWrapper.eq("a.deleted",0);
        IPage<SysRoleDTO> list = baseMapper.findList(new Page<>(1, 1), queryWrapper);
        if(list!=null&&list.getRecords()!=null&&list.getRecords().size()>0){
            return list.getRecords().getFirst();
        }
        return null;
    }
    /**
     * 删除机构关联
     * @Date 2023/9/6 9:57
     * @Author luzhengning
     **/
    public boolean deleteByOrgIds(List<String> orgIds){
        if(ObjectUtils.isEmpty(orgIds))return false;
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.in("sys_org",orgIds);
        return this.remove(queryWrapper);
    }
}
